import 'package:bloc/bloc.dart';
import 'package:built_collection/built_collection.dart';
import 'package:mobile_app.core/core/domain/entities/tts_voice.dart';
import 'package:mobile_app.core/core/domain/utils/with_previous_stream.dart';

part 'bloc_state.dart';

class TtsBloc extends Cubit<TtsBlocState?> {
  TtsBloc() : super(null) {
    stream = super.stream.cast();
    streamWithPrevious = stream.withPrevious;
  }

  @override
  late final Stream<TtsBlocState> stream;

  late final Stream<(TtsBlocState, TtsBlocState?)> streamWithPrevious;

  void initialize({
    required BuiltList<TtsVoice> availableAmVoices,
    required BuiltList<TtsVoice> availableBrVoices,
  }) async {
    if (state != null) {
      return;
    }

    emit(TtsBlocState(
      availableAmVoices: availableAmVoices,
      availableBrVoices: availableBrVoices,
    ));
  }

  void updateAvailableVoices({
    required BuiltList<TtsVoice> availableAmVoices,
    required BuiltList<TtsVoice> availableBrVoices,
  }) async {
    if (state == null) {
      return;
    }

    emit(state!.copyWith(
      availableAmVoices: () => availableAmVoices,
      availableBrVoices: () => availableBrVoices,
    ));
  }
}
